/**
 * 区域
 * 文件命名格式：zmn.模块名称.表名称.功能.jas
 * @author duanzucai
 * @since 2018/10/30
 */
layui.extend({
    zmnExportUtil: '{/}/static/js/zmn.excel.util'
}).define(['index', 'form', 'table', 'laypage', 'layedit', 'laydate','zmnExportUtil'], function (exports) {
    var $ = layui.$;
    var table = layui.table;
    var laypage = layui.laypage;
    let form = layui.form;
    let zmnExportUtil = layui.zmnExportUtil;

    table.init('zmn-table', {
        toolbar: "#zmn-table-tool",
        page: true,
        limit: json.pageSize,
        height: 'full-140',
        done: function () {
            $("div[lay-event='LAYTABLE_EXPORT']").attr('lay-event', 'CUSTOM_EXPORT');
        }
    });

    function tableRender(){
        laypage.render(zmnPage.laypageOptions(json));
    }
    tableRender();

    var ids = new Array();

    // 选择（全选或单选）
    table.on("checkbox(zmn-table)", function () {
        ids = table.checkStatus('zmn-table').data.map((item) => item.areaId);
    });
    // 启用/停用
    table.on("toolbar(zmn-table)", function (obj) {
        if (obj.event != "start" && obj.event != "stop" && obj.event != 'CUSTOM_EXPORT') {
            return;
        }

        if (obj.event == "start" || obj.event == "stop") {
            if (ids.length === 0) {
                return layer.alert("请选择至少一条数据！");
            }
            zmnPage.layStartStop(obj, "/area/updateStatus.action", ids.join(","));
        }

        //导出excel
        if (obj.event == 'CUSTOM_EXPORT'){
            zmnExportUtil.doExportExcel('zmn-table','区域管理');
        }
    });

    // 修改排序
    table.on("edit(zmn-table)", function (obj) {
        if (obj.field == 'sort') {
            zmnPage.laySort(obj, "/area/updateSort.action", obj.data.areaId);
        } else if (obj.field=='firstLetter') {
            zmnPage.layFirstLetter(obj.value,"/area/updateFirstLetter.action",obj.data.areaId,3,1000);
        } else if (obj.field=='simpleSpell') {
            zmnPage.laySimpleSpell(obj.value,"/area/updateSimpleSpell.action",obj.data.areaId,3,1000);
        }
    });

    //查询条件重置
    form.on('submit(zmn-form-reset)', function (obj) {
        form.val("zmn-area-filter", {
            "nameOrAreaId": ""
        });
        return false;
    });

    $("body").on("click",".editArea",function () {
        let parentId = $(this).attr("parentid");
        let areaId = $(this).attr("areaid");
        let url = '/area/detail.action';
        //添加的时候parentId不为空，修改的时候areaId不为空
        if(parentId){
            url = url +'?parentId='+parentId;
        }else{
            url = url +'?areaId='+areaId;
        }
        let data = {
            title: '',
            shade: 0.8,
            maxmin: false,
            resize: false,
            area: ['60%', '80%'],
            closeBtn: 1
        };
        zmnLayUtil.openIframeCustom(layer, url, data);
    });

    form.on('switch(chk_status)', function(data){
        let obj = new Object();
        if (data.elem.value == 2){
            obj.event = 'stop';
        } else{
            obj.event = 'start';
        }
        zmnPage.layStartStop(obj, "/area/updateStatus.action", data.elem.name);
    });

    //对外暴露的接口
    exports('art_area_list', {});
});